package com.example.seakdreamandroid.sleep.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;

public class MediaUtil {

    private static MediaPlayer mMediaPlayer;
    private static boolean open =true;

    //开始播放
    public static void playRing(Context context){
        try {
            if (open == true) {
                //用于获取手机默认铃声的Uri
                Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setDataSource(context, alert);
                //告诉mediaPlayer播放的是铃声流
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
                mMediaPlayer.setLooping(true);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
                open = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //停止播放
    public static void stopRing(){
        if (mMediaPlayer!=null){
            open = false;
            if (mMediaPlayer.isPlaying()){
                mMediaPlayer.stop();
                mMediaPlayer.release();
                open = true;
            }
        }
    }
}
